package dk.dashboardFP.panels.displayPanel.marks {
	import dk.dashboardFP.core.DashboardFPSettings;
	import dk.dashboardFP.core.DashboardFPCore;
	import dk.dashboardFP.DashboardFP;
	import dk.dashboardFP.panels.ui.text.DFPLabel;
	import dk.dashboardFP.shapes.Box;
	import dk.dashboardFP.utils.describeObjectUtil.DescribeObjectUtil;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author josef
	 */
	public class AbstractObjectMark extends Sprite {
		protected var _doTarget : DisplayObject;
		protected var _doAsset : MarkAsset;
		private var _mcTL : MovieClip;
		private var _mcBL : MovieClip;
		private var _mcBR : MovieClip;
		private var _mcTR : MovieClip;
		protected var _sprContainer : Sprite;
		private var _mcCenter : MovieClip;
		protected var _doLabel : DFPLabel;
		protected var _doLabelBack : Box;
		private var _sprCover : Sprite;

		protected var _rectBounds : Rectangle;
		private var _sprLabelHolder : Sprite;
		private var _objectType : String;

		public function AbstractObjectMark(doTarget : DisplayObject, sprContainer : Sprite) {
			
			_sprContainer = sprContainer;
			_doTarget = doTarget;
			
			setupElements();
		}

		protected function setupElements() : void {
			
			//:::::::
			// mc asset 
			_doAsset = new MarkAsset();
			addChild(_doAsset);
			
			_mcTL = _doAsset.getChildByName("mcLT") as MovieClip;
			_mcTR = _doAsset.getChildByName("mcRT") as MovieClip;
			_mcBR = _doAsset.getChildByName("mcRB") as MovieClip;
			_mcBL = _doAsset.getChildByName("mcLB") as MovieClip;
			
			_mcCenter = _doAsset.getChildByName("mcCenter") as MovieClip;
			
			//:::::::
			// cover 
			_sprCover = new Sprite();
			addChild(_sprCover);
			
			//:::::::
			// label holder 
			_sprLabelHolder = new Sprite();
			addChild(_sprLabelHolder);
			
			//:::::::
			// label 
			_doLabel = new DFPLabel("", true);
			_doLabel.y = 2;
			
			_doLabelBack = new Box(_doLabel.width + 4, _doLabel.height, 0x888888, 0.85);
			
			_sprLabelHolder.addChild(_doLabelBack);
			_sprLabelHolder.addChild(_doLabel);
			
			// set type 
			_objectType = DescribeObjectUtil.getObjectDescription(_doTarget).className;
			
			//:::::::
			// init 
			updateMark();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// update
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		protected function updateMark() : void {
			try {
				//:::::::
				// place corners
				_rectBounds = _doTarget.getBounds(_sprContainer);
			 
				_mcTL.x = _rectBounds.left;
				_mcTL.y = _rectBounds.top;
			 
				_mcTR.x = _rectBounds.right;
				_mcTR.y = _rectBounds.top;
			 
				_mcBR.x = _rectBounds.right;	
				_mcBR.y = _rectBounds.bottom;
			 
				_mcBL.x = _rectBounds.left;
				_mcBL.y = _rectBounds.bottom;
			
				//:::::::
				// draw cover 
				_sprCover.graphics.clear();
				_sprCover.graphics.beginFill(0x000000, 1);
				_sprCover.graphics.drawRect(_rectBounds.x, _rectBounds.y, _rectBounds.width, _rectBounds.height);
				_sprCover.alpha = DashboardFPSettings.get_oInstance().selectionAlphaProp.nValue;
			
				//:::::::
				// place center 
				var pCenter : Point = _sprContainer.globalToLocal(_doTarget.parent.localToGlobal(new Point(_doTarget.x, _doTarget.y)));
			
				_mcCenter.x = pCenter.x;
				_mcCenter.y = pCenter.y;
			
			
				//:::::::
				// label 
				_sprLabelHolder.x = pCenter.x;
				_sprLabelHolder.y = pCenter.y;

				_doLabel.sText = _objectType + " ( " + _doTarget.x + " , " + _doTarget.y + " )";
			
				_doLabelBack.width = _doLabel.width + 4;
				_doLabelBack.height = _doLabel.height;
			}catch(error : Error) {
				hide();	
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// hide show
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function hide() : void {
			_sprCover.visible = false;
			_sprLabelHolder.visible = false;
			_doAsset.visible = false;
		}

		public function show() : void {
			_sprCover.visible = true;
			_sprLabelHolder.visible = true;
			_doAsset.visible = true;
		}
	}
}
